package cc.xuzhi.muggle.common.controller;

import cc.xuzhi.muggle.common.exception.ServiceException;
import cc.xuzhi.muggle.common.model.Result;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class BaseController {

    @ExceptionHandler
    public Result handleServiceException(ServiceException e) {
        if(e instanceof ServiceException) {
            e.printStackTrace();
            return StringUtils.hasLength(e.getMessage()) ? Result.error(e.getMessage()) : Result.error("请求失败");
        }else {
            e.printStackTrace();
            return Result.error("内部服务器异常");
        }
    }

    @ExceptionHandler
    public Result handleException(Exception e) {
        if(e instanceof ServiceException) {
            e.printStackTrace();
            return StringUtils.hasLength(e.getMessage()) ? Result.error(e.getMessage()) : Result.error("请求失败");
        }else {
            e.printStackTrace();
            return Result.error("内部服务器异常");
        }
    }
}
